<?php
namespace app\member\model;

use app\BaseModel;
use app\massage\model\CouponRecord;
use think\facade\Db;

class Rights extends BaseModel
{
    //会员权益表
    protected $name = 'longbing_card_v2_member_rights';


    protected $append = [

        'status_text'

    ];


    /**
     * @author chenniang
     * @DataTime: 2020-07-16 11:51
     * @功能说明:状态
     */
    public function getStatusTextAttr($value,$data){

        if(!empty($data['id'])){

            $relation_model = new RightsRelation();

            $info = $relation_model->where(['rights_id'=>$data['id']])->find();

            return !empty($info)?1:0;

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-07-15 09:49
     * @功能说明:添加
     */
    public function rightsAdd($data){

        $data['status']      = 1;

        $data['create_time'] = time();

        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-16 11:57
     * @功能说明:初始化
     */
    public function initData($uniacid){

        $dis = [

            'uniacid' => $uniacid,

            'type'    => 0

        ];

        $data = $this->rightsInfo($dis);

        if(empty($data)){

            $arr = [

                [
                    'title'      => '消费折扣',

                    'show_title' =>  '优享会员价',

                    'type'       => 0,

                    'content'    => '',

                    'top'        => 10,

                    'icon'       => 'https://longbingcdn.xiaochengxucms.com/admin/car/discount.png',

                    'uniacid'    => $uniacid,

                    'use_type'   => 1,

                    'status'     => 1

                ],[
                    'title'      => '积分倍率',

                    'show_title' =>  '会员多积分',

                    'type'       => 0,

                    'content'    => '',

                    'top'        => 10,

                    'icon'       => 'https://longbingcdn.xiaochengxucms.com/admin/car/multiple.png',

                    'uniacid'    => $uniacid,

                    'use_type'   => 2,

                    'status'     => 1
                ],[
                    'title'      => '赠送积分',

                    'show_title' =>  '会员专享积分',

                    'type'       => 0,

                    'content'    => '',

                    'top'        => 10,

                    'icon'       => 'https://longbingcdn.xiaochengxucms.com/admin/car/integral.png',

                    'uniacid'    => $uniacid,

                    'use_type'   => 3,

                    'status'     => 1,

                    'is_up'      => 1

                ],
                [

                    'title'      => '会员送券',

                    'show_title' =>  '送卡券',

                    'type'       => 0,

                    'content'    => '',

                    'top'        => 10,

                    'icon'       => 'https://longbingcdn.xiaochengxucms.com/admin/car/coupon.png',

                    'uniacid'    => $uniacid,

                    'use_type'   => 4,

                    'status'     => 1,

                    'is_up'      => 1
                ]
            ];

            $this->saveAll($arr);

        }


        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:12
     * @功能说明:列表
     */
    public function rightsList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;
    }




    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:08
     * @功能说明:详情
     */
    public function rightsInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:13
     * @功能说明:编辑
     */
    public function rightsUpdate($dis,$data){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-22 16:58
     * @功能说明:自定义权益
     */
    public function  customRights($member_id){

        $dis[]= ['b.member_id','=',$member_id];

        $dis[]= ['b.type','=',1];

        $dis[]= ['a.status','=',1];

        $data = $this->alias('a')
                ->join('longbing_card_v2_member_rights_relation b','a.id = b.rights_id')
                ->where($dis)
                ->group('b.rights_id')
                ->column('a.title');

        return array_values($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-28 10:38
     * @功能说明:会员权益
     */
    public function memberRights($level_id,$uniacid){

        $dis = [

            'b.member_id' => $level_id,

            'b.uniacid'   => $uniacid,

            'a.status'    => 1
        ];

        $data = $this->alias('a')
                ->join('longbing_card_v2_member_rights_relation b','a.id = b.rights_id')
                ->where($dis)
                ->field(['a.id','a.show_title','a.type','a.use_type','a.icon'])
                ->group('a.id')
                ->order(['a.top desc','a.id desc'])
                ->select()
                ->toArray();

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-29 10:49
     * @功能说明:商品等级下拉框
     */
    public function goodsLevelSelect($dis){

        $data = $this->alias('a')
                ->join('longbing_card_member_rights_relation b','a.id = b.rights_id')
                ->join('longbing_card_member_level c','c.id = b.member_id')
                ->where($dis)
                ->field('c.title,c.id,c.top')
                ->group('c.id')
                ->order('c.top')
                ->select()
                ->toArray();

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-12 17:40
     * @功能说明:给用户权益
     */
    public function giveRights($level_id,$user_id,$order_id=0){

        $log_model = new RightsLog();

        $level_model = new Level();

        $level = $level_model->levelInfo(['id'=>$level_id]);

        if(empty($level)){

            return false;
        }

        $dis = [

            'user_id' => $user_id,

            'level_id'=> $level_id,

            'uniacid' => $level['uniacid']
        ];
        //已经给或该权益了
        $find = $log_model->rightsInfo($dis);

        if(!empty($find)){

            return false;
        }

        $log_model->insert($dis);
        //赠送积分
        if(!empty($level['send_integral'])&&!empty($level['send_integral_switch'])){

            $point_model = new Integral();

            $point_model->integralUserAdd($user_id,$level['send_integral'],$level['uniacid'],2,0,$order_id);
        }
        //赠送卡券
        //优惠券
        $coupon_model= new Coupon();

        $coupon_record_model = new CouponRecord();

        $dis = [

            'member_id' => $level_id
        ];
        //优惠券
        $coupon = $coupon_model->where($dis)->select()->toArray();

        if(!empty($coupon)&&!empty($level['coupon_switch'])){

            foreach ($coupon as &$value){

                $coupon_record_model->recordAdd($value['coupon_id'],$user_id,$value['num']);

            }

        }


        return true;

    }







}